// pages/cart/cart.js
import {Cart} from 'cart-model.js';
var cart = new Cart();

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onHide: function (options) {
    wx.setStorageSync(cart._storageKey, this.data.cartData)
  },

  /**
   * 生命周期函数--监听页面显示
   * 每次切换到该页面都会执行
   */
  onShow: function () {
    // 获取缓存中购物车的数据
    var cartData = cart.getCartDataFromLocal();
    // 解析购物车信息：选中的总金额、商品数量、商品类型数量
    var cartTotalInfo = this._getSelectProductTotalInfo(cartData);
    
    this.setData({
      cartData : cartData,
      cartTotalInfo : cartTotalInfo
    });
  },

  // 解析订单信息
  _getSelectProductTotalInfo: function (data) {
    var price = 0.00,
    productCount = 0,
    typeCount = 0;

    let multiple = 100;

    if (data){
      for (let i=0; i < data.length; i++){
        if (data[i].selectStatus) {
          price += data[i].counts * multiple * Number(data[i].price) * multiple;
          productCount += data[i].counts
          typeCount++
        }
      }
    }
    return {
      price : price / (multiple * multiple),
      productCount : productCount,
      typeCount: typeCount
    };
  },

  // 购物车商品勾选动作
  toggleSelect: function (event) {
    var changeStatus = cart.getDataSet(event, 'status'),
      id = cart.getDataSet(event, 'id'),
      cartData = this.data.cartData;

      for (let i = 0; i < cartData.length; i++) {
        let item = cartData[i];
        if (item.id == id){
          item.selectStatus = changeStatus;
          break;
        }
      }

      this.data.cartData = cartData;
      // 重置购物车计算数据
      this._resetCartData();
      // 重新设置购物车缓存
      // wx.setStorageSync(cart._storageKey, cartData)
  },


  // 选择所有商品
  toggleSelectAll:function (event){
    var changeStatus = cart.getDataSet(event, 'status'),
    cartData = this.data.cartData;
    for (let i = 0; i < cartData.length; i++) {
      cartData[i].selectStatus = !changeStatus;
    }
    this.data.cartData = cartData;
     // 重置购物车计算数据
     this._resetCartData();
     // 重新设置购物车缓存
     wx.setStorageSync(cart._storageKey, cartData)
  },

  // 删除商品
  delCartProduct:function (event) {
    var id = cart.getDataSet(event, 'id'),
    index = cart.getCartProductIndex(id, this.data.cartData);

    if (index >= 0){
      this.data.cartData.splice(index, 1);
      // 重置购物车计算数据
      this._resetCartData();
      // 重新设置购物车缓存
      wx.setStorageSync(cart._storageKey, this.data.cartData)
    }

  },

  changeProductCounts: function (event) {
    var id = cart.getDataSet(event, 'id'),
    type = cart.getDataSet(event, 'type'),
    index = cart.getCartProductIndex(id, this.data.cartData),
    result = false, count = 0;

    // 判断当前商品ID是否在购物车中
    if (index != -1){
      if (type == 'add') {
        result = cart.addCounts(index);
        count  = 1;
      }else {
        result = cart.reduceCounts(index);
        count  = -1;
      }
    }

    // 如果修改成功,则更新数据进行绑定
    if (result){
      this.data.cartData[index].counts += count;
      this._resetCartData();
    }

  },

  // 重置购物车计算数据
  _resetCartData:function (){
    // 解析购物车信息：选中的总金额、商品数量、商品类型数量
    var cartTotalInfo = this._getSelectProductTotalInfo(this.data.cartData);
    this.setData({
      cartData : this.data.cartData,
      cartTotalInfo : cartTotalInfo
    });
  },

  // 跳转倒订单页面
  submitOrder:function (event){
    wx.navigateTo({
      url: '../order/order?totalPrice=' + this.data.cartTotalInfo.price + '&from=cart' ,
    })
  }
})